import {Component, OnInit} from '@angular/core';
import { IonicPage,NavController, NavParams, ToastController} from 'ionic-angular';
// import {MePage} from "../me/me";
import {PhoneService} from "../../service/http-service/phone.service";
import {ApplicantService} from "../../service/http-service/applicant.service";
import {ValidateUtils} from "../../util/validate-utils";

/**
 * Generated class for the PhonePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-phone',
  templateUrl: 'phone.html',
})
export class PhonePage implements OnInit{
  phoneNum:string;
  code:string;
  usrInfo:string;
  toastCtrl:ToastController;
  // showToast = false;
  // toastText = '';
  isShowed:boolean;//获取验证码的请求是否得到回馈的标识
  ngOnInit(): void {
    // console.log("444444")
  }

  constructor(public navCtrl: NavController, public navParams: NavParams,public  phoneService:PhoneService ,
              public  applicationService:ApplicantService,
              public toastService:ToastController,
              private validate: ValidateUtils,) {
    this.toastCtrl=toastService;
  }

  // ionViewDidLoad() {
  //   // console.log('ionViewDidLoad PhonePage');
  //
  // }
// 验证码倒计时
  verifyCode: any = {
    verifyCodeTips: "获取验证码",
    countdown: 60,
    disable: true
  }

  // 倒计时
  settime() {
    if (this.verifyCode.countdown == 1) {
      this.verifyCode.countdown = 60;
      this.verifyCode.verifyCodeTips = "获取验证码";
      this.verifyCode.disable = true;
      return;
    } else {
      --(this.verifyCode.countdown);
    }

    this.verifyCode.verifyCodeTips = "重新获取(" + this.verifyCode.countdown + ")";
    var timeout = setTimeout(() => {
      this.verifyCode.verifyCodeTips = "重新获取(" + this.verifyCode.countdown + ")";
      this.settime();
    }, 1000);
    var i=0;
    if (this.isShowed){
      clearTimeout(timeout);
      this.verifyCode.countdown = 60;
      this.verifyCode.verifyCodeTips = "获取验证码";
      this.verifyCode.disable = true;
      console.log(i++);
      this.isShowed=false;
    }
  }

  /**
   * 验证手机验证码是否正确
   */
  open_me() {
    var phoneNum=this.phoneNum;
    var code=this.code;
    if (
      !this.phoneNum ||
      !this.code
    ) {
      // console.log('请将信息填写完整');
      this.validate.showtoast('请将信息填写完整',this.toastCtrl,'middle');
      return;
    } else if (!this.validate.phone(this.phoneNum)) {
      // console.log('手机格式不正确');
      this.validate.showtoast('手机格式不正确',this.toastCtrl,'middle');
      return;
    }
    this.phoneService.verifySmsCode(phoneNum,code).subscribe(resp=>{
      console.log("检查验证码是否正确");
      if (resp.data==null&& resp.code==200){
        console.log("检查验证码正确");
        this.bindPhoneNum();
      }else if(resp.data!==null &&resp.code==1401){
        this.applicationService.freshTokenn();
      }else {
        this.validate.showtoast(resp.msg,this.toastCtrl,'middle');
      }
    },error2 => {
      console.log(error2);
    })
  };

  /**
   * 获取手机验证码
   */
  getCode(){
    if (
      !this.phoneNum
    ) {
      console.log('请将信息填写完整');
      this.validate.showtoast('请将信息填写完整',this.toastCtrl,'middle');
      return;
    } else if (!this.validate.phone(this.phoneNum)) {
      console.log('手机格式不正确');
      this.validate.showtoast('手机格式不正确',this.toastCtrl,'middle');
      return;
    }
    console.log(this.phoneNum);
    var phoneNum=this.phoneNum;
    this.verifyCode.disable = false;
    this.settime();
    this.phoneService.postCode(phoneNum).subscribe(resp=>{
      if (resp.code==200){
        this.validate.showtoast( resp.msg,this.toastCtrl,'middle');
        this.isShowed=true;

      } else if(resp.data!==null &&resp.code==1401){
        this.applicationService.freshTokenn();
      }else{
        this.validate.showtoast( resp.msg,this.toastCtrl,'middle');
        this.isShowed=true;
      }
    },error2 => {
      console.log(error2);
    })
  }

  bindPhoneNum(){
    this.phoneService.bindPhone(this.phoneNum).subscribe(resp=>{
      if (resp.data!==null&& resp.code==200){
        this.usrInfo=resp.data;
        var str = JSON.stringify(this.usrInfo);
        localStorage.setItem("options",str);
        localStorage.setItem("phone",resp.data.phone);
        console.log(localStorage.getItem('options'));
        this.navCtrl.push('MePage');

      }else if(resp.data!==null &&resp.code==1401){
        this.applicationService.freshTokenn();
      }else {
        this.validate.showtoast( resp.msg,this.toastCtrl,'middle');
      }
    },error2 => {
      console.log(error2);
    })
  }

  // Toast(text: string) {
  //   if(!text || text.length == 0){
  //     text = '未知错误';
  //   }
  //   this.toastText = text;
  //   this.showToast = true;
  //   setTimeout(() => {
  //     this.showToast = false;
  //   }, 1500);
  // }
}
